package cn.hzc.fortess.core.shiro.service.impl;


import cn.hzc.fortess.core.shiro.service.PermissionCheckService;
import cn.hzc.fortess.core.shiro.ShiroKit;
import cn.hzc.fortess.core.shiro.ShiroUser;
import org.apache.shiro.authz.annotation.Logical;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(readOnly = true)
public class PermissionCheckServiceImpl implements PermissionCheckService {

    @Override
    public boolean checkResourceAuth(Object[] permissions, Logical logical) {
        ShiroUser user = ShiroKit.getUser();
        if (null == user) {
            return false;
        }
        if (user.isAdministrator()) {
            return true;
        }

        if (permissions == null || permissions.length == 0) {
            return false;
        }
        if (logical.equals(Logical.AND)) {
            return ShiroKit.hasAllPermission(permissions);
        }
        return ShiroKit.hasAnyPermission(permissions);

    }

    @Override
    public boolean checkRoleAuth(Object[] permissions, Logical logical) {
        ShiroUser user = ShiroKit.getUser();
        if (null == user) {
            return false;
        }
        if (user.isAdministrator()) {
            return true;
        }

        if (permissions == null || permissions.length == 0) {
            return false;
        }
        if (logical.equals(Logical.AND)) {
            return ShiroKit.hasAllRoles(permissions);
        }
        return ShiroKit.hasAnyRoles(permissions);

    }
}
